package org.example.studentsystem_back.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.example.studentsystem_back.common.CommonResponse;
import org.example.studentsystem_back.domain.Money;
import org.example.studentsystem_back.dto.PageDTO;
import org.example.studentsystem_back.service.MoneyService;
import org.example.studentsystem_back.vo.moneyMangeVo.MoneyVo;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@Valid
@RestController
public class MoneyController {
    @Resource
    private MoneyService moneyService;

    @PostMapping("/money")
    public CommonResponse<MoneyVo> findMoney(@RequestBody String json) {
        JSONObject jsonObject = JSON.parseObject(json);
        PageDTO pageDTO=jsonObject.toJavaObject(PageDTO.class);
        String a=jsonObject.getString("num");
        int num;
        if(a==null|| a.isEmpty()){
            num=-1;
        }
        else {
            num=Integer.parseInt(a);
        }
        String name=jsonObject.getString("name");
        return moneyService.findAll(pageDTO,num,name);
    }

    @PostMapping("/money/add")
    public CommonResponse<Money> addMoney(@Valid @RequestBody Money money) {
        return moneyService.add(money);
    }

    @PostMapping("/money/update")
    public CommonResponse<Money> updateMoney(@Valid @RequestBody Money money) {
        return moneyService.update(money);
    }

    @PostMapping("/money/delete")
    public CommonResponse<Money> deleteMoney(@Valid @RequestBody Money money) {
        return moneyService.delete(money);
    }
}
